<template>
  <Spin :spinning="loading" tip="Loading...">
  <div>
    <!-- 紧急返件回运 - 编辑 -->
    <BreakSpace label="质量信息" :containerStyle="{ marginBottom: '10px' }"/>
    <FormPanel ref="formPanel1" :list="formList" :cols="3" :formType="'add'" labelWidth="110" :isSubmitBtn="false"
               @formChange="formChangeHandle"/>
    <div style="margin-bottom: 1rem;"></div>
    <BreakSpace label="返件信息" :containerStyle="{ marginBottom: '10px' }"/>
    <FilterTable
      ref="table3"
      :columns="columns"
      :params="fetchParams"
      :datakey="'items'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
      height='auto'
    >
    </FilterTable>
    <BreakSpace label="回运信息" :containerStyle="{ marginBottom: '10px' }"/>
    <FormPanel ref="formPanel2" :list="formList3" :cols="3" :formType="'add'" labelWidth="100" :isSubmitBtn="false"
               @formChange="formChangeHandle2"/>
    <div style="padding-top: 3rem;"></div>
    <div
      :style="{
        position: 'fixed',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <multiuse-button type="primary" :click="saveDialog.bind()" :disabled="flag">保存</multiuse-button>
<!--      <multiuse-button type="primary" :click="huiyun.bind()" :disabled="flag">回运</multiuse-button>-->
      <multiuse-button type="primary" @click="closeDialog">关 闭</multiuse-button>
    </div>
    <div style="margin-bottom: 1rem;"></div>
  </div>
  </Spin>
</template>
<script>
import {quyrpQaUrgent,getPartType,editSave} from '@/api/claim/dealer/QualityInformationManagement_DLR/EmergencyReturn/index';
import { isFormEmpty } from '@/utils/index';
import { copyUpdateData } from '@/utils/index';
import moment from "moment";
import { dictionary } from '@/utils/dictMixin';
export default {
  mixins: [dictionary],
  props: ['rowData'],
  components: {
  },
  data() {
    return {
      loading: false,
      editRow: {},
      dataList: [],
      collapse: false,
      urgentStatusFlag: 5,
      infoId: null,
      fetchParams: {}, //ipa 接口
      selectedRows: [], //选中经销商
      formList: this.createFormList(),
      formList3: this.createFormList3(),
      columns: this.creatHeader(),
      saveData: null,
      flag:false,
    };
  },
  async created() {
    this.loading = true;
    await this.chuqi();
    await this.getDetailFun(this.rowData.urgentId);
    if(this.rowData.statusStr =='已回运'){
      this.formList.find(x => x.fieldName == 'str').hidden = false;
      this.formList.find(x => x.fieldName == 'str').initialValue = this.rowData.statusStr;
      this.flag = true;
      this.formList3.map(x => {
          x.disabled = true;
      });
    }else if(this.rowData.urgentStatus==55651007||this.rowData.urgentStatus==55651008){
      this.formList.find(x => x.fieldName == 'str').hidden = true;
      this.formList.find(x => x.fieldName == 'urgentStatus').hidden = false;
      this.flag = true;
      this.formList3.map(x => {
        x.disabled = true;
      });
    }
    else{
      this.formList.find(x => x.fieldName == 'str').hidden = true;
      this.formList.find(x => x.fieldName == 'urgentStatus').hidden = false;
    }
    this.loading = false;
  },
  methods: {
    //保存
    // baocun(){
    //   this.urgentStatusFlag=1;
    //   this.saveDialog();
    // },
    // //回运
    // huiyun(){
    //   this.urgentStatusFlag=2;
    //   this.saveDialog();
    // },
    //考核类型
    async chuqi() {
      // const res = await getPartType({ type: 5521 });
      // if (res.resultCode == 200) {
      //   this.formList3.find(x => x.fieldName == 'expressType').itemList = res.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
      // const res3 = await getPartType({type: 5564});
      // if (res3.resultCode == 200) {
      //   this.formList.find(x => x.fieldName == 'dataSource').itemList = res3.data.map(y => ({
      //     text: y.codeCnDesc,
      //     value: y.codeId
      //   }));
      // }
      // const res4 = await getPartType({type: 5565});
      // if (res4.resultCode == 200) {
      //   this.formList.find(x => x.fieldName == 'urgentStatus').itemList = res4.data.map(y => ({
      //     text: y.codeCnDesc,
      //     value: y.codeId
      //   }));
      // }
      // const res5 = await getPartType( {type:5500});
      // if (res5.resultCode == 200) {
      //   this.formList.find(x => x.fieldName == 'isKeep').itemList = res5.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
    },
    async getDetailFun(val) {
      let res= await quyrpQaUrgent(val);
      this.editRow = res.data;
      let data = res.data;
      if(data.saleDate){
        data.saleDate = moment(data.saleDate).format('YYYY-MM-DD');
      }
      if(data.prodDate){
        data.prodDate = moment(data.prodDate).format('YYYY-MM-DD');
      }
      //为form赋值
      for (let i in res.data) {
        this.formList3.map(x => {
          if (x.fieldName == i) {
            x.initialValue = res.data[i];
          }
        });
        this.formList3.forEach(x => {
          x.initialValue = data[x.fieldName];
          if (x.fieldName == 'fileList') {
            x.initialValue = data.fileList.map((f) => ({ name: f.title, url: f.fileUrl }));
          }
          if (x.fieldName == 'expressName') {
            x.initialValue = data.expressName==null?'顺丰':data.expressName;
          }
          if (x.fieldName == 'expressType') {
            x.initialValue = data.expressType==null?55211003:data.expressType;
          }
        });
      }
      for (let i in res.data) {
        this.formList.map(x => {
          if (x.fieldName == i) {
            x.initialValue = res.data[i];
          }
        });
      }

      let fileIds = [];
      res.data.fileList.map(x =>{
        fileIds.push(x.fileId);
      });
      let idsss = '';
      fileIds.map(x => {
        idsss += x + ','
      });
      this.fileIds = idsss;

      console.log(6666);
      //为table赋值
      if (200 == res.resultCode) {
        let data = res.data;
        if (isFormEmpty(data)) {
          data = {};
        }
        let records = data.dtlList;
        console.log(records);
        if (isFormEmpty(records)) {
          records = [];
        }
        this.$refs.table3.$refs.pageTable.EXECUTE_INSERT(records);
      }

      this.newVar =data;
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '配件代码', dataIndex: 'partCode', sorter: true },
        { title: '配件名称', dataIndex: 'partName', sorter: true },
        { title: '单位', dataIndex: 'partUnit', sorter: true },
        { title: '数量', dataIndex: 'partCount', sorter: true },
        { title: '收件人', dataIndex: 'receiver', sorter: true },
        { title: '收件人电话', dataIndex: 'receiverTel', sorter: true },
        { title: '收件地址', dataIndex: 'receiverAddress', sorter: true },
        {title: '快递公司', dataIndex: 'courier', sorter: true,},
        {title: '返件仓库', dataIndex: 'factory', sorter: true,},
      ];
    },
    //基本信息
    createFormList() {
      return [
        {
          type: 'INPUT',
          label: '紧急返件单号',
          fieldName: 'urgentNo',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '质量报告单号',
          fieldName: 'reportNo',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '数据来源',
          fieldName: 'dataSource',
          readonly: true,// 只读
          disabled: true, // 置会
          itemList: this.createDictList('5564'),
        },
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '紧急返件单状态',
          fieldName: 'str',
          readonly: true,// 只读
          disabled: true, // 置会
          hidden:false
        },
        {
          type: 'SELECT',
          label: '紧急返件单状态',
          fieldName: 'urgentStatus',
          readonly: true,// 只读
          disabled: true, // 置会
          itemList: this.createDictList('5565'),
          hidden:true
        },
        {
          type: 'INPUT',
          label: '维修工单号',
          fieldName: 'roNo',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '索赔单号',
          fieldName: 'cmsClaimNo',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '生产日期',
          fieldName: 'prodDate',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '销售日期',
          fieldName: 'saleDate',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '主损件',
          fieldName: 'partCode',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'INPUT',
          label: '主损件名称',
          fieldName: 'partName',
          readonly: true,// 只读
          disabled: true // 置会
        },
        {
          type: 'SELECT',
          label: '是否留车',
          fieldName: 'isKeep',
          placeholder: '请输入...',
          disabled:true,
          itemList: this.createDictList('5500'),
        },
        {
          type: 'INPUT',
          label: '留车单号',
          fieldName: 'keepCar',
          placeholder: '请输入数量...',
          disabled: true, // 置会
        },
      ];
    },
    createFormList3() {
      return [
        {
          type: 'SELECT',
          label: '运输方式',
          fieldName: 'expressType',
          itemList: this.createDictList('5521'),
          rules: [{required: true, message: '请输入运输方式', trigger: 'change'}],
          readonly: true,// 只读
        },
        {
          type: 'INPUT',
          label: '物流公司',
          fieldName: 'expressName',
          rules: [{required: true, message: '请输入物流公司', trigger: 'change'}]
        },
        {
          type: 'INPUT',
          label: '物流单号',
          fieldName: 'expressNo',
          rules: [{required: true, message: '请输入物流单号', trigger: 'change'}]
        },
        {
          type: 'DATE',
          label: '回运时间',
          fieldName: 'expressDate',
          placeholder: '选择回运时间',
          valueFormat: 'yyyy-MM-dd', //返回数据格式
          rules: [{ required: true, message: '请选择回运时间', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '邮寄人',
          fieldName: 'expressUser',

          rules: [{ required: true, message: '请选择邮寄人', trigger: 'change' }]
        },
        {
          type: 'INPUT',
          label: '邮寄人电话',
          fieldName: 'expressTel',
        },
        {
          type: 'INPUT',
          label: '邮寄说明',
          fieldName: 'expressRemarks',
          selfCols: 3,// 元素占据几行
          rows: 1
        },
        {
          type: 'UPLOAD_FILE',
          label: '上传文件',
          fieldName: 'fileList',
          placeholder: '上传文件...',
          initialValue: [],
          upload: {
            actionUrl: '/api/file/oss/upload',
            limit: 2,
            // tipText: '只能上传pdf格式'
          }
        }
      ];
    },
    formChangeHandle2(val) {
      this.saveData = val;
    }, formChangeHandle(val) {
      this.saveData = val;
    },
    //搜索获取数据
    changeHandle(val) {
      this.fetchParams = val;
      console.log(val);
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    //保存
    async saveDialog() {
      this.$refs.formPanel2.SUBMIT_FORM();
      let fileList = [];
      this.saveData.fileList.map((x) => {
        fileList.push({ title: x.name, fileUrl: x.url });
      });
      this.saveData.delFileIds = this.fileIds;
      this.saveData.fileList = fileList;

      if (this.saveData !== null) {

        this.editRow = Object.assign(this.editRow, this.saveData);

          this.editRow.urgentStatus=55651003;
        this.newVar = copyUpdateData(this.newVar,this.editRow);
        this.loading = true;
        let res = await editSave(this.newVar);
        console.log(res);
        if (res.resultCode === 200) {
          this.closeDialog('refresh');
          return this.$notify.success({ title: '提示信息', message: '保存成功' });
        } else {
          this.loading = false;
          this.$notify.error({ title: '提示信息', message: res.errMsg || '保存失败!' });
          this.closeDialog();
        }
        this.loading = false;
      } else {
        return this.$notify.warning({ title: '提示信息', message: '有未校验通过的信息' });
      }
    },
    //取消操作
    closeDialog() {
      this.$emit('close', {visible: false, data: this.selectedRows, type: 'close'});
    }
  }
};
</script>

<style lang="less" scoped></style>
